#!usr/bin/env python 
# -*- coding: utf-8 -*-
import datetime
import os,types


__author__ = '锄禾'

log_tag = os.path.basename(os.getcwd())

def save_crawler_log(log_path, log):
    log_file = open(log_path + 'CrawlerLog.txt', 'a')
    if type(log) is types.CodeType.co_consts:
        if "Step" in log:
            log_str = log_tag + " : " + str(datetime.datetime.now()) + " " + log
        else:
            log_str = log_tag + " : " + str(datetime.datetime.now()) + "       - " + log
    else:
        log_str = log_tag + " : " + str(datetime.datetime.now()) + "       - " + str(log)

    print(log_str)
    log_file.write(log_str + '\n')
    del log_str, log_path, log
    log_file.close()

def save_crawler_log_both(plan_log_path, device_log_path, log):
    save_crawler_log(plan_log_path, log)
    save_crawler_log(device_log_path, log)

def save_logcat(plan, device):
    save_crawler_log(plan.logPath, u"Step：保存设备日志：" + device.id)
    if not os.path.exists(os.getcwd()):
        os.makedirs(os.getcwd())
    command = 'adb -s '+ device.id + ' logcat -d >> ' + device.logPath + '/logcat.txt'
    os.system(command)
    del plan, device, command